//
//  AddingDeviceView.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/5/20.
//

import SwiftUI
import FlowStacks

struct AddingDeviceView: View {
    @EnvironmentObject var navigator: FlowNavigator<HomeStack>
    @StateObject var viewModel = AddingDeviceViewModel()
    var body: some View {
        NavigationBar(title: "添加设备")
        VStack{
            Text("正在添加设备")
                .font(.system(size: 16, weight: .semibold))
                .foregroundColor(AppColors.Gy1)
                .padding(.top, 24)
            Text("确保 Wi-Fi 信号良好")
                .font(.system(size: 14, weight: .regular))
                .foregroundColor(AppColors.Gy2)
                .padding(.top, 5)
            ZStack{
                RotatingScannerView(isAnimating: true, imageName: "circle_back", size: 248)
                    .padding(.top, 24)
                Image("icon_center").resizable().scaledToFit().frame(width: 84, height: 84)
            }
            StepProgressView(viewModel: viewModel)
                .padding(.top, 55)
                .padding(.horizontal, 28)
            
            Button("xiayibu"){
                viewModel.advanceStep()
            }
        }
        Spacer()
        
    }
}



struct StepIcon: View {
    let step: SetupStep
    let status: StepStatus

    var body: some View {
        let imageName: String = {
            switch status {
            case .noState: return "no_state"
            case .ingState: return "ing_state"
            case .doneState: return "done_state"
            }
        }()

        ZStack {
            if status == .ingState {
                TimelineView(.animation) { context in
                    VStack{
                        Image(imageName)
                            .resizable()
                            .frame(width: 32, height: 32)
                            .rotationEffect(.degrees(context.date.timeIntervalSinceReferenceDate * 180))
                        Text(step.title)
                            .font(.system(size: 14, weight: .semibold))
                            .foregroundColor(AppColors.Brand6)
                    }
                   
                }
            } else {
                VStack{
                    Image(imageName)
                        .resizable()
                        .frame(width: 32, height: 32)
                    Text(step.title)
                        .font(.system(size: 14))
                        .foregroundColor(AppColors.Gy2)
                }
            }
        }
    }
}

struct StepProgressView: View {
    @ObservedObject var viewModel: AddingDeviceViewModel
    
    var body: some View {
        ZStack(alignment: .top){
            Rectangle()
                .frame(height: 2)
                .foregroundColor(AppColors.Brand6)
                .frame(width: 320)
                .padding(.top, 15)
            HStack(alignment: .center) {
                ForEach(SetupStep.allCases.indices, id: \.self) { index in
                    let step = SetupStep.allCases[index]
                    let status = viewModel.stepStatuses[index]
                    
                    StepIcon(step: step, status: status)
                    if index != SetupStep.allCases.count - 1 {
                              Spacer()
                    }
                }
            }
     
        }
        
    }
}

enum SetupStep: Int, CaseIterable {
    case authentication
    case networking
    case cloudConnection
    case completion

    var title: String {
        switch self {
        case .authentication: return "设备认证"
        case .networking: return "设备联网"
        case .cloudConnection: return "设备入云"
        case .completion: return "设备成功"
        }
    }
}

enum StepStatus {
    case noState     // 未开始状态
    case ingState    // 进行中状态
    case doneState   // 已完成状态
}
